package cn.jit.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;

import cn.jit.util.MailUtil;
import cn.jit.util.RedisUtil;
import cn.jit.util.ResponseObject;
import cn.jit.util.ResponseUtil;

@Controller
public class BaseController {
	
	/******************* 前端界面 开始 ********************/
	/**
	 * 网站首页
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String homePage(Model model, HttpServletRequest request) {

		return "index";
	}
	
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String loginPage(Model model, HttpServletRequest request) {

		return "login";
	}
	
	@RequestMapping(value = "/register", method = RequestMethod.GET)
	public String registerPage(Model model, HttpServletRequest request) {

		return "register";
	}
	
	@RequestMapping(value = "/forgetpassword", method = RequestMethod.GET)
	public String forgetPwdPage(Model model, HttpServletRequest request) {

		return "forget_password";
	}
	
	
	/******************* 前端界面 结束 ********************/
	
	
	/******************* 后端接口 开始 ********************/
	
	@RequestMapping("/yzm")
    @ResponseBody
    public void getIdentifyingCode(HttpServletRequest request, HttpServletResponse response) throws IOException{
    	//设置页面不缓存
		response.setHeader("pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		
		//在内存中创建图像
		int width = 60,height = 20;
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		
		
		//获得图形上下文
		Graphics g = image.getGraphics();
		
		//创建随机数对象
		Random random = new Random();
		
		//设置背景色
		g.setColor(new Color(255, 255, 255));
		g.fillRect(0, 0, width, height);
		
		//设定字体
		g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
		
		//随机的干扰线
		for(int i=0; i<200; i++) {
			int x = random.nextInt(width);
			int y = random.nextInt(height);
			int x1 = random.nextInt(12);
			int y1 = random.nextInt(12);
			g.drawLine(x, y, x+x1, y+y1);
		}
		
		//随机的四位数字
		String sRand = "";
		for (int i=0; i<4; i++) {
			String rand = String.valueOf(random.nextInt(10));
			sRand += rand;
			
			//将验证码显示到图像中
			g.setColor(new Color(30 + random.nextInt(160), 40 + random.nextInt(170), 40 + random.nextInt(180)));
			g.drawString(rand, 13 * i + 6, 16);
		}
		
		//将验证码保存在session里
		request.getSession().setAttribute("yzm", sRand);
		
		g.dispose();
		
		ImageIO.write(image, "JPEG", response.getOutputStream());
    }
	
	@RequestMapping("/mail_check")
	public void getMailCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String email = request.getParameter("email");
		// 验证邮箱合法性
		if (!email.matches("^\\w+@(\\w+\\.)+\\w+$")) {
			ResponseObject returnInfo = new ResponseObject();
			returnInfo.setCode(200);
			returnInfo.setIsSuccess(false);
			returnInfo.setMsg("邮箱格式有误!");
			ResponseUtil.writeJson(response, returnInfo);
			return;
		}
		
		// 产生随机码
		String sRand = "";
		Random random = new Random();
		for (int i = 0; i < 4; i++) {
			String rand = String.valueOf(random.nextInt(10));
			sRand += rand;
		}
		
//		System.out.println(RedisUtil.set("access_token", "sha1md5base64")); //OK
//		System.out.println(RedisUtil.expire("access_token", 123)); //1
//		System.out.println(RedisUtil.expire("access_tokenn", 123)); // 0
//		System.out.println(RedisUtil.get("access_toke")); //null
		
		// 存redis,设置过期时间
		String userKey = email + "_code";
		RedisUtil.set(userKey, sRand);
		RedisUtil.expire(userKey, 300);
		
		
		String title = "用户验证";
		String content = "这是一封用于用户验证的邮件";
		new Thread(new MailUtil(email, sRand,title,content)).start();
		
		ResponseObject returnInfo = new ResponseObject();
		returnInfo.setCode(200);
		returnInfo.setIsSuccess(true);
		returnInfo.setMsg("邮件发送成功!");
		ResponseUtil.writeJson(response, new Gson().toJson(returnInfo));
		return;
	}
	/******************* 后端接口 结束 ********************/
}
